import java.util.Stack;

public class TwoStackQueue{
	private Stack<Integer> pushStack;
	private Stack<Integer> popStack;

	public TwoStackQueue(){
		this.pushStack = new Stack<Integer>();
		this.popStack = new Stack<Integer>();
	}

	public void add(int value){
		this.pushStack.push(value);
	}

	public int poll(){
		this.reverse();
		return this.popStack.pop();
	}

	public int peek(){
		this.reverse();
		return this.popStack.peek();
	}

	private void reverse(){
		if( this.popStack.empty() && this.pushStack.empty() ){
			throw new RuntimeException("queue is empty");
		} else if( this.popStack.empty() ){
			while( !this.pushStack.empty() ){
				this.popStack.push( this.pushStack.pop() );
			}
		}
	}

	public static void main(String[] args){
		TwoStackQueue queue = new TwoStackQueue();
		queue.add(1);
		queue.add(2);
		queue.add(3);
		queue.add(4);
		queue.add(5);

		System.out.println(queue.poll());
		System.out.println(queue.poll());
		System.out.println(queue.poll());
		System.out.println(queue.poll());
		System.out.println(queue.poll());
	}	
}
